//
//  XMLRefreshControl.swift
//  Weibo
//
//  Created by mac on 16/5/17.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

let myHeight :CGFloat = 50

enum MyRefreshState :Int {

    //下拉刷新状态
    case Normal = 0
    //松手就刷新状态
    case Pulling = 1
    //刷新状态
    case Refreshing = 2
}

class XMLRefreshControl: UIControl {
    
    var currentScrollView :UIScrollView?
    
    var myRefreshState :MyRefreshState = .Normal {
    
        didSet {
        
            switch myRefreshState {
            
            case .Normal:
//                print("Normal")
                if oldValue == .Refreshing {
                
                    currentScrollView?.contentInset.top -= myHeight
                }
            case .Pulling:
                print("Pulling")
            case .Refreshing:
//                print("Refreshing")
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.currentScrollView?.contentInset.top += myHeight
                })
                sendActionsForControlEvents(.ValueChanged)
            }
        }
    }
    
    func endRefresh() {
    
        myRefreshState = .Normal
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
    
        backgroundColor = UIColor.orangeColor()
    }
    
    override func willMoveToSuperview(newSuperview: UIView?) {
//        print("-------")
//        print(newSuperview)
        
        guard let scrollView = newSuperview as? UIScrollView else {
        
            return
        }
        
        scrollView.addObserver(self, forKeyPath: "contentOffset", options: [.New], context: nil)
        currentScrollView = scrollView
        
        frame.size.width = currentScrollView!.frame.size.width
        frame.size.height = myHeight
        frame.origin.y = -myHeight
        
        
    }
    
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
//        print(currentScrollView?.contentOffset.y)
        
        if currentScrollView!.dragging {
            //拖动状态下只有两种状态 1.下拉刷新  2.松手就刷新
//            print("拖动")
            let maxY = -(currentScrollView!.contentInset.top + myHeight)
            let currentOffsetY = currentScrollView!.contentOffset.y
            if currentOffsetY < maxY && myRefreshState == .Normal {
                //松手就刷新状态(pulling)
                myRefreshState = .Pulling
//                print("pulling")
            }else if currentOffsetY >= maxY && myRefreshState == .Pulling {
                //下拉刷新状态(normal)
                myRefreshState = .Normal
//                print("normal")
            }
            
        }else {
            //要想进入刷新状态，必须是松手就刷新状态，还得松手
        
//            print("松手")
            if myRefreshState == .Pulling {
            
                myRefreshState = .Refreshing
            }
        }
        
    }
    
    
    deinit {
    
        currentScrollView?.removeObserver(self, forKeyPath: "contentOffset")
    }

}
